// Function to load XML file
function loadXMLDoc(dname)
{
    if (window.XMLHttpRequest)
    {
        xhttp=new XMLHttpRequest();
    }
    else
    {
        xhttp=new ActiveXObject("Microsoft.XMLHTTP");
    }
    xhttp.open("GET",dname,false);
    xhttp.send("");
    return xhttp.responseXML;
}

// Function to create prefix for xml file
function nsResolver(prefix) {
    return prefix == 'n' ? 'http://xml.netbeans.org/schema/informations' : null;
}

// Function to search by type | category
function searchByType(type){

    // Delete childNode;
    deleteDiv(type);

    var xml=loadXMLDoc("xml/information.xml");
    //var searchValue = document.getElementById("txtSearchValue").value;
    var path="//n:information[contains(n:type,'"+type+"')]";
    
    // code for Mozilla, Firefox, Opera, etc.
    if (document.implementation && document.implementation.createDocument) {
        var nodes = xml.evaluate(path, xml, nsResolver, XPathResult.ANY_TYPE, null);
        var result = nodes.iterateNext();
        var array = new Array();
        while (result) {
            array[0] = result.getElementsByTagName("id")[0].childNodes[0].nodeValue;
            array[1] = result.getElementsByTagName("owner")[0].childNodes[0].nodeValue;
            array[2] = result.getElementsByTagName("info")[0].childNodes[0].nodeValue;
            array[3] = result.getElementsByTagName("description")[0].childNodes[0].nodeValue;
            array[4] = result.getElementsByTagName("date")[0].childNodes[0].nodeValue;
            array[5] = result.getElementsByTagName("category")[0].childNodes[0].nodeValue;
            addDiv(array);
            result = nodes.iterateNext();
        }

    }
}

// Function to add new child div in parent div
function addDiv(array){
    var div = document.createElement("div");
    div.setAttribute("class", "question_list");
    div.setAttribute("id", "question");
    div.innerHTML = "<h3><a href='detail.jsp?id="+array[0]+"' rel='bookmark'>"+array[5]+ array[0]+ " - "+ array[1] + " - "+ array[2] +"</a></h3><span class='post-time'>"+array[4]+"</span><br/><p><span class='pcate'> "+ array[3].substring(0, 95) +"...</span></p>";
    var pa = document.getElementById("parent");
    pa.appendChild(div);
}

// Function to delete child of parent div
function deleteDiv(value){
    var parent = document.getElementById("parent");
    parent.innerHTML = "";
    var h1 = document.createElement("h1");
    h1.setAttribute("class", "page_head");
    //var searchValue = document.getElementById("txtSearchValue").value;
    h1.innerHTML = "Kết quả tìm kiếm - " + value;
    var pa = document.getElementById("parent");
    pa.appendChild(h1);
}

// Convert character from Vietnamese to unsigned
function convertVietnamese(str){
    str= str.toLowerCase();
    str= str.replace(/à|á|ạ|ả|ã|â|ầ|ấ|ậ|ẩ|ẫ|ă|ằ|ắ|ặ|ẳ|ẵ/g,"a");
    str= str.replace(/è|é|ẹ|ẻ|ẽ|ê|ề|ế|ệ|ể|ễ/g,"e");
    str= str.replace(/ì|í|ị|ỉ|ĩ/g,"i");
    str= str.replace(/ò|ó|ọ|ỏ|õ|ô|ồ|ố|ộ|ổ|ỗ|ơ|ờ|ớ|ợ|ở|ỡ/g,"o");
    str= str.replace(/ù|ú|ụ|ủ|ũ|ư|ừ|ứ|ự|ử|ữ/g,"u");
    str= str.replace(/ỳ|ý|ỵ|ỷ|ỹ/g,"y");
    str= str.replace(/đ/g,"d");
    return str;
}

// Function search in file information_convert.xml, non Vietnamese
function searchInFileConvert(){
    var str = document.getElementById("txtSearchValue").value;
    var searchValue = convertVietnamese(str);

    var xml=loadXMLDoc("xml/information_convert.xml");
    deleteDiv(str);
    var path="//n:information[contains(n:description,'"+searchValue+"') or contains(n:fullName,'"+searchValue+"') or contains(n:owner,'"+searchValue+"') or contains(n:info,'"+searchValue+"') or contains(n:phone,'"+searchValue+"')]";
   
    // code for Mozilla, Firefox, Opera, etc.
     if (document.implementation && document.implementation.createDocument) {
        var nodes = xml.evaluate(path, xml, nsResolver, XPathResult.ANY_TYPE, null);
        var result = nodes.iterateNext();
      
        while (result) {
            var id = result.getElementsByTagName("id")[0].childNodes[0].nodeValue;

            // Call function searchVietnameseFile() to get Vietnamese data.
            searchVietnameseFile(id);
            result = nodes.iterateNext();
        }
    }
}

// Function search in file information.xml
function searchVietnameseFile(id){

    var xml=loadXMLDoc("xml/information.xml");

    var path="//n:information[n:id = '"+id+"']";

    // code for Mozilla, Firefox, Opera, etc.
    if (document.implementation && document.implementation.createDocument) {
        var nodes = xml.evaluate(path, xml, nsResolver, XPathResult.ANY_TYPE, null);
        var result = nodes.iterateNext();
        var array = new Array();

        // Get value of childNode
        array[0] = result.getElementsByTagName("id")[0].childNodes[0].nodeValue;
        array[1] = result.getElementsByTagName("owner")[0].childNodes[0].nodeValue;
        array[2] = result.getElementsByTagName("info")[0].childNodes[0].nodeValue;
        array[3] = result.getElementsByTagName("description")[0].childNodes[0].nodeValue;
        array[4] = result.getElementsByTagName("date")[0].childNodes[0].nodeValue;
        array[5] = result.getElementsByTagName("category")[0].childNodes[0].nodeValue;

        // Call function addDiv()
        addDiv(array);
    }
}
